package problema081;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;

public class PathSum {
	
	static int[][] matriz;
	static int[][] sol;
	
	public static void main(String[] args){
		System.out.println(minPathSum());
	}
	
	public static int minPathSum(){
		leer();
		sol[79][79] = matriz[79][79];
		resolver(0,0);
		return sol[0][0];
	}
	
	public static int resolver(int x, int y){
		if(x == 80 || y == 80) return Integer.MAX_VALUE;
		if(sol[x][y] != 0){
			return sol[x][y];
		}else{
			sol[x][y] = minimo(resolver(x+1,y),resolver(x,y+1)) + matriz[x][y];
			return sol[x][y];
		}
	}

	public static int minimo(int x, int y){
		if(x < y) return x;
		return y;
	}
	
	public static void leer(){
		matriz = new int[80][80];
		sol = new int[80][80];
		try{
			BufferedReader reader = new BufferedReader( new FileReader("matrizEj81.txt") );
			String linea;
			int fila = 0;
			while ((linea = reader.readLine()) != null){
				StringTokenizer st = new StringTokenizer(linea, "," );
				int col = 0;
				while (st.hasMoreTokens()){
					matriz[fila][col] = Integer.parseInt(st.nextToken().trim());
					col++;
				}
				fila++;
			}
			reader.close();
			}catch (Exception e){
				System.err.println("Error: " + e.getMessage());
			}
	}
}
